package com.xmu.edu.tisp.database;

//a general database service class to access SQLite
//By Chenbo 2011-08-06

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.xmu.edu.tisp.dao.TISP_dbHelper;
import com.xmu.edu.tisp.login.TispLogin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BaseService implements DbService {
	
	protected static SQLiteDatabase myDB;
	protected static SQLiteOpenHelper dbHelper;
	
	protected String tableName;
	protected String primaryKey;
	
	public BaseService(){
	}
	
	public static SQLiteDatabase getdb()
	{
		if(myDB==null)
		{
			dbHelper=new TISP_dbHelper(TispLogin.context, "TISP");
			myDB=dbHelper.getWritableDatabase();
			return myDB;
		}
		return myDB;
	}
	
	public void update(ContentValues values) {
		
		getdb().update(tableName, values, primaryKey+"=?", new String[]{(String) values.get(primaryKey)});
		System.out.println("update @"+values.getAsString(primaryKey) );
	} 
	
	public void insert(ContentValues values) {
		getdb().insert(tableName, null, values);
		System.out.println("insert @"+values.getAsString(primaryKey) );
	}
	
	public void insertOrUpdate(ContentValues values){
		String pk=values.getAsString(primaryKey);
		if(pk==null) return;
		Cursor c = getdb().query(tableName, new String[] { primaryKey },
				primaryKey + "=?", new String[] { pk }, null, null, null);
		boolean exist = c.moveToNext();
		c.close();
		if (exist) {// already exist
			update(values);
			return;
		}
		insert(values);
	}
	
	public void delete(String id) {
		getdb().delete(tableName, primaryKey+"=?", new String[]{id});
		System.out.println("delete @ "+id +" table "+tableName);
	}
	
	public void dealXJSZXX(ContentValues cv)
	{
		String state = cv.getAsString("DATA_STATE").trim();
		String hth=cv.getAsString("HTH");
		String nd=cv.getAsString("ND");
		String pzdm=cv.getAsString("PZDM");
		boolean exists;
		Cursor c=getdb().rawQuery("select * " +
				"from XJSZXX " +
				"where HTH=? "+
				"and ND=? "+
				"and PZDM=?;", new String[]{hth,nd,pzdm});
		exists=c.moveToNext();
		c.deactivate();
		c.close();
		cv.remove("DATA_STATE");
		if("1".equals(state))
		{
			getdb().delete("XJSZXX", "HTH=? and "+"ND=? and "+"PZDM=?", new String[]{hth,nd,pzdm});
		}
		else
		{
			if(exists)
			{
				getdb().update("XJSZXX", cv, "HTH=? and "+"ND=? and "+"PZDM=?", new String[]{hth,nd,pzdm});
			}
			else
				getdb().insert("XJSZXX", null, cv);
		}
	}
	
	public List<String> getIDs() {
		Cursor c=getdb().query(tableName, new String[]{primaryKey},null, null, null, null, null);
		List<String> result=new ArrayList<String>();
		while(c.moveToNext())
		{
			String id=c.getString(c.getColumnIndex(primaryKey));
			result.add(id);
		}
		c.close();
		return result;
	}

	@Override
	public ContentValues queryById(String id) {
		Cursor c=getdb().query(tableName, null ,primaryKey+"=?",new String[]{id}, null, null, null);
		if(c.moveToNext()){
			ContentValues cv=readOneRecord(c);
			c.close();
			return cv;
		}
		return new ContentValues();
	}
	
	public static ContentValues readOneRecord(Cursor cursor){
		ContentValues cv=new ContentValues();
		for(int i=0;i<cursor.getColumnCount();++i){
			cv.put(cursor.getColumnName(i), cursor.getString(i));
		}
		return cv;
	}
	
	public static DbService createService(String tableName,String primaryKey){
		//System.out.println("tableName"+tableName+",primaryKey"+primaryKey);
		BaseService service=new BaseService();
		service.tableName=tableName;
		service.primaryKey=primaryKey;
		return service;
	}
	
	public static DbService createService(String tableName){
		return createService(tableName,Tables.primaryKey(tableName));
	}
	
	public static ContentValues getSingleRecord(String tableName){
		Cursor c=getdb().query(tableName,null, null,null,null,null,null);
		if(c.moveToNext()){
			ContentValues cv=readOneRecord(c);
			c.close();
			return cv;
		}
		return null;
	}
	
	public static void insertOrUpdate(String tableName,ContentValues values){
		createService(tableName,Tables.primaryKey(tableName)).insertOrUpdate(values);
	}
	
	public static List<ContentValues> list(String tableName){
		List<ContentValues> list=new LinkedList<ContentValues>();
		Cursor c=getdb().query(tableName, null,null,null,null,null,null);
		while(c.moveToNext()){
			list.add(readOneRecord(c));
		}
		c.close();
		return list;
	}
}
